﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

/// <summary>
/// Summary description for TblStats
/// </summary>
namespace HNG.Portal.BLL
{
    public class TblStats
    {
        public TblStats()
        {            
        }
        public static int Update(int in_OnlineNumber)
        {
            SqlConnection connect = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            connect.Open();
            SqlCommand cmd = new SqlCommand("tblStats_SelectAll", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            int i = 0;
            Stats Total;
            Stats[] _stats=new Stats[7];
            using(IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    i = Convert.ToInt32(reader["StatTypeID"]) - 1;
                    _stats[i] = new Stats(reader); 
                }
            }
            connect.Close();
            connect.Dispose();
            ExecuteUpdate(_stats);
            return in_OnlineNumber;
        }
        private static void ExecuteUpdate(Stats[]  in_Stats)
        {
            SqlConnection connect = new SqlConnection(HNG.LIB.Common.Connection.strConn);
            connect.Open();
            SqlCommand cmd;
            DateTime _TodayDate = in_Stats[0]._Time;
            if (String.Format("{0:MM/dd/yyyy}",_TodayDate) == String.Format("{0:MM/dd/yyyy}",DateTime.Now))
            {
                cmd = new SqlCommand("tblStats_Update", connect);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.ExecuteNonQuery();
                connect.Close();
                connect.Dispose();
            }
            else
            {
                // Update Total
                cmd = new SqlCommand("Update tblStats SET Count=Count+1 WHERE StatTypeID=7", connect);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                _TodayDate = Convert.ToDateTime(String.Format("{0:MM/dd/yyyy}", _TodayDate));
                DateTime tmpDateTime = Convert.ToDateTime(String.Format("{0:MM/dd/yyyy}", DateTime.Now));
                TimeSpan tmpTimeSpan = tmpDateTime - _TodayDate;
                // Update ToDay Date
                cmd = new SqlCommand("Update tblStats SET Time=Convert(datetime,'" + DateTime.Now.ToShortDateString() + "',102) WHERE StatTypeID=1", connect);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                // Check for 1 day range
                if (tmpTimeSpan.Days == 1)
                {
                    // Set Yesterday Count
                    cmd = new SqlCommand("Update tblStats SET Count=" + in_Stats[0]._Count.ToString() + " WHERE StatTypeID=2", connect);
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    // Reset ToDay Count
                    cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=1", connect);
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    // Check for First of Week
                    if (DateTime.Now.DayOfWeek==DayOfWeek.Monday)
                    {
                        // Update last week count = current week count
                        cmd = new SqlCommand("Update tblStats SET Count=" + in_Stats[2]._Count.ToString() + " WHERE StatTypeID=4", connect);
                        cmd.CommandType = CommandType.Text;
                        cmd.ExecuteNonQuery();
                        // Reset current week count = 0
                        cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=3", connect);
                        cmd.CommandType = CommandType.Text;
                        cmd.ExecuteNonQuery();
                    }
                    // Check for First of Month
                    if (DateTime.Now.Day==1)
                    {
                        // Update last month count = current month count
                        cmd = new SqlCommand("Update tblStats SET Count=" + in_Stats[4]._Count.ToString() + " WHERE StatTypeID=6", connect);
                        cmd.CommandType = CommandType.Text;
                        cmd.ExecuteNonQuery();
                        // Reset current month count = 0
                        cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=5", connect);
                        cmd.CommandType = CommandType.Text;
                        cmd.ExecuteNonQuery();
                    }
                }
                else // if range is more than 1 day
                {
                    // Reset Yesterday=0
                    cmd = new SqlCommand("Update tblStats SET Count=0 WHERE StatTypeID=2", connect);
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    // Reset ToDay Count
                    cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=1", connect);
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    if (DateTime.Now.Year - _TodayDate.Year >=2 )
                    {
                        // Reset ToDay,YesterDay,ThisWeek,LastWeek,ThisMonth,LastMonth Count
                        cmd = new SqlCommand("Update tblStats SET Count=0 WHERE StatTypeID=1 OR StatTypeID=2 OR StatTypeID=3 OR StatTypeID=4 OR StatTypeID=5 OR StatTypeID=6", connect);
                        cmd.CommandType = CommandType.Text;
                        cmd.ExecuteNonQuery();
                        // Increase ToDay,ThisWeek,ThisMonth Count
                        cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=1 OR StatTypeID=3 OR StatTypeID=5", connect);
                        cmd.CommandType = CommandType.Text;
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        // Check for year range
                        if (DateTime.Now.Year - _TodayDate.Year ==1)
                        {
                            if (DateTime.Now.Month==1 && _TodayDate.Month==12)
                            {
                                if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
                                {
                                    // Update last week count = current week count
                                    cmd = new SqlCommand("Update tblStats SET Count=" + in_Stats[2]._Count.ToString() + " WHERE StatTypeID=4", connect);
                                    cmd.CommandType = CommandType.Text;
                                    cmd.ExecuteNonQuery();
                                    // Reset current week count = 0
                                    cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=3", connect);
                                    cmd.CommandType = CommandType.Text;
                                    cmd.ExecuteNonQuery();
                                }
                                // Check for First of Month
                                if (DateTime.Now.Day == 1)
                                {
                                    // Update last month count = current month count
                                    cmd = new SqlCommand("Update tblStats SET Count=" + in_Stats[4]._Count.ToString() + " WHERE StatTypeID=6", connect);
                                    cmd.CommandType = CommandType.Text;
                                    cmd.ExecuteNonQuery();
                                    // Reset current month count = 0
                                    cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=5", connect);
                                    cmd.CommandType = CommandType.Text;
                                    cmd.ExecuteNonQuery();
                                }
                            }
                            else
                            {
                                // Reset ToDay,YesterDay,ThisWeek,LastWeek,ThisMonth,LastMonth Count
                                cmd = new SqlCommand("Update tblStats SET Count=0 WHERE StatTypeID=1 OR StatTypeID=2 OR StatTypeID=3 OR StatTypeID=4 OR StatTypeID=5 OR StatTypeID=6", connect);
                                cmd.CommandType = CommandType.Text;
                                cmd.ExecuteNonQuery();
                                // Increase ToDay,ThisWeek,ThisMonth Count
                                cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=1 OR StatTypeID=3 OR StatTypeID=5", connect);
                                cmd.CommandType = CommandType.Text;
                                cmd.ExecuteNonQuery();
                            }
                        }
                        else // if year range = 0
                        {
                            if (DateTime.Now.Month - _TodayDate.Month >=2)
                            {
                                // Reset ToDay,YesterDay,ThisWeek,LastWeek,ThisMonth,LastMonth Count
                                cmd = new SqlCommand("Update tblStats SET Count=0 WHERE StatTypeID=1 OR StatTypeID=2 OR StatTypeID=3 OR StatTypeID=4 OR StatTypeID=5 OR StatTypeID=6", connect);
                                cmd.CommandType = CommandType.Text;
                                cmd.ExecuteNonQuery();
                                // Increase ToDay,ThisWeek,ThisMonth Count
                                cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=1 OR StatTypeID=3 OR StatTypeID=5", connect);
                                cmd.CommandType = CommandType.Text;
                                cmd.ExecuteNonQuery();
                            }
                            else // if month range is less than 1
                            {
                                if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
                                {
                                    // Update last week count = current week count
                                    cmd = new SqlCommand("Update tblStats SET Count=" + in_Stats[2]._Count.ToString() + " WHERE StatTypeID=4", connect);
                                    cmd.CommandType = CommandType.Text;
                                    cmd.ExecuteNonQuery();
                                    // Reset current week count = 0
                                    cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=3", connect);
                                    cmd.CommandType = CommandType.Text;
                                    cmd.ExecuteNonQuery();
                                }
                                // Check for First of Month
                                if (DateTime.Now.Day == 1 || DateTime.Now.Day < in_Stats[0]._Time.Day)
                                {
                                    // Update last month count = current month count
                                    cmd = new SqlCommand("Update tblStats SET Count=" + in_Stats[4]._Count.ToString() + " WHERE StatTypeID=6", connect);
                                    cmd.CommandType = CommandType.Text;
                                    cmd.ExecuteNonQuery();
                                    // Reset current month count = 0
                                    cmd = new SqlCommand("Update tblStats SET Count=1 WHERE StatTypeID=5", connect);
                                    cmd.CommandType = CommandType.Text;
                                    cmd.ExecuteNonQuery();
                                }
                                cmd = new SqlCommand("Update tblStats SET Count=0 WHERE StatTypeID=2", connect);
                                cmd.CommandType = CommandType.Text;
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }
                }
                connect.Close();
                connect.Dispose();
            }
        }
    }
    public class Stats
    {
        public  int _ID;
        public int _TypeID;
        public double _Count;
        public DateTime _Time;
        public Stats()
        {
        }
        public Stats(IDataReader reader)
        {
            _ID = Convert.ToInt32(reader["ID"]);
            _TypeID = Convert.ToInt32(reader["StatTypeID"]);
            _Count = Convert.ToDouble(reader["Count"]);
            _Time = Convert.ToDateTime(reader["Time"]);
        }
    }
}
